package com.gxg.web.Controller;

import com.gxg.config.R;
import com.gxg.config.code.RCode;
import com.gxg.dao.UserMapper;
import com.gxg.pojo.User;
import com.gxg.service.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.HashMap;

/**
 * Created by guxiaoguang on 2018/4/23.
 */
@Api(description = "首页Login")
@Controller
@RequestMapping(value = "/manager")
public class managerController {

    @Autowired
    private LoginService loginService;


    @Autowired
    private StringRedisTemplate template;


    @RequestMapping(value = "login", method = RequestMethod.POST, produces = "application/json")
    public @ResponseBody
    R login(@Valid @RequestBody User user, BindingResult result)
            throws Exception {
        User login = loginService.selectUserByName(user.getName());
        if (!login.getAddress().equals("md")) {
            return R.error().put(RCode.CODE, RCode.ERROR_VERIFY_CODE)
                    .put(RCode.MESSAGE, RCode.ERROR_VERIFY_MSG);
        }
        return R.ok().put(RCode.CODE, "0000");
    }

    /**
     * 可以直接使用@ResponseBody响应JSON
     *
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getcount", method = RequestMethod.POST)
    @ApiOperation(value="测试-getCount")
    public ModelMap getCount(HttpServletRequest request,
                             HttpServletResponse response) {
        ModelMap map = new ModelMap();
        ValueOperations<String, String> ops = template.opsForValue();

        String name = ops.get("name");
        System.out.println(name+"------------");
        map.addAttribute("count", 158);

        // 后台获取的国际化信息
        map.addAttribute("xstest", "测试");
        return map;
    }
}
